/*
 * Copyright (c) 2010-2011, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.wordpress;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.SwingWorker;

import org.panteleyev.blogapi.Blog;
import org.panteleyev.blogapi.WordPressAccount;
import org.panteleyev.blogapi.WordPressComment;
import org.panteleyev.blogclient.Icon;
import org.panteleyev.blogclient.MainClass;
import org.panteleyev.utilities.Updatable;

/**
 *
 * @author Petr Panteleyev <petr@panteleyev.org>
 */
class CommentHandler {
    public abstract static class CommentReplyAction extends AbstractAction {
        public CommentReplyAction() {
            super(L10N.COMMENT_REPLY_ACTION.s(), Icon.COMMENT_REPLY.i());
            putValue(Action.SHORT_DESCRIPTION, L10N.COMMENT_REPLY_ACTION.s());
            putValue(Action.LARGE_ICON_KEY, Icon.COMMENT_REPLY.I());
        }
    }

    public abstract static class CommentDeleteAction extends AbstractAction {
        public CommentDeleteAction() {
            super(L10N.COMMENT_DELETE_ACTION.s(), Icon.COMMENT_DELETE.i());
            putValue(Action.SHORT_DESCRIPTION, L10N.COMMENT_DELETE_ACTION.s());
            putValue(Action.LARGE_ICON_KEY, Icon.COMMENT_DELETE.I());
        }
    }

    public abstract static class CommentSpamAction extends AbstractAction {
        public CommentSpamAction() {
            super(L10N.COMMENT_SPAM_ACTION.s(), Icon.CANCEL.i());
            putValue(Action.SHORT_DESCRIPTION, L10N.COMMENT_SPAM_ACTION.s());
            putValue(Action.LARGE_ICON_KEY, Icon.CANCEL.I());
        }
    }

    public abstract static class CommentEditAction extends AbstractAction {
        public CommentEditAction() {
            super(L10N.COMMENT_EDIT_ACTION.s(), Icon.COMMENT_EDIT.i());
            putValue(Action.SHORT_DESCRIPTION, L10N.COMMENT_EDIT_ACTION.s());
            putValue(Action.LARGE_ICON_KEY, Icon.COMMENT_EDIT.I());
        }
    }

    public abstract static class CommentNewAction extends AbstractAction {
        public CommentNewAction() {
            super(L10N.COMMENT_NEW_ACTION.s(), Icon.NEW_POST.i());
            putValue(Action.SHORT_DESCRIPTION, L10N.COMMENT_NEW_ACTION.s());
        }
    }

    public abstract static class CommentApproveAction extends AbstractAction {
        public CommentApproveAction() {
            super(L10N.COMMENT_APPROVE_ACTION.s(), Icon.APPROVE.i());
            putValue(Action.SHORT_DESCRIPTION, L10N.COMMENT_APPROVE_ACTION.s());
            putValue(Action.LARGE_ICON_KEY, Icon.APPROVE.I());
        }
    }

    public abstract static class CommentHoldAction extends AbstractAction {
        public CommentHoldAction() {
            super(L10N.COMMENT_HOLD_ACTION.s(), Icon.HOLD.i());
            putValue(Action.SHORT_DESCRIPTION, L10N.COMMENT_HOLD_ACTION.s());
            putValue(Action.LARGE_ICON_KEY, Icon.HOLD.I());
        }
    }

    private static String approveColor = "#99FFCC";
    private static String holdColor = "#FFFFCC";
    private static String spamColor = "#FFCCCC";

    private final WordPressAccount account;
    private final Updatable callback;

    public CommentHandler(WordPressAccount account, Updatable callback) {
        this.account = account;
        this.callback = callback;
    }

    public String commentToString(WordPressComment comm) {
        if (comm == null) {
            return null;
        } else {
            StringBuilder b = new StringBuilder("<html><body bgcolor='white'>");

            String color = approveColor;

            switch (comm.getStatus()) {
                case HOLD:
                    color = holdColor;
                    break;
                case SPAM:
                    color = spamColor;
                    break;
            }

            b.append("<table border='0' width='100%'>")
                .append("<tr bgcolor='")
                .append(color)
                .append("' valign='top'><td>")
                .append("<b>").append(comm.getAuthor()).append("</b>");

            if (comm.getAuthorEmail() != null && !comm.getAuthorEmail().isEmpty()) {
                b.append("&nbsp; &lt;").append(comm.getAuthorEmail()).append(">");
            }

            if (comm.getAuthorUrl() != null && !comm.getAuthorUrl().isEmpty()) {
                b.append(String.format("<br><a href=\"%s\">%s</a>", comm.getAuthorUrl(), comm.getAuthorUrl()));
            }

    //        if (comm.getDate() != null) {
    //            b.append("<br>");
    //            b.append(dateFormat.format(comm.getDate()));
    //        }

            if (comm.getIp() != null) {
                b.append("<br>(");
                b.append(comm.getIp());
                b.append(")");
            }

            b.append("<tr><td colspan='2'>");

            if (comm.getContent() != null) {
                b.append(comm.getContent());
            }

            b.append("</table>");

            b.append("</body></html>");
            return b.toString().replaceAll("\n", "<br>");
        }
    }

    void deleteComments(final Blog blog, final ArrayList<WordPressComment> comments) {
        new SwingWorker<Object,Object>() {
            @Override
            protected Object doInBackground() throws Exception {
                for (WordPressComment c : comments) {
                    account.deleteComment(blog, c);
                }
                return null;
            }

            @Override
            protected void done() {
                try {
                    get();
                } catch (InterruptedException | ExecutionException ex) {
                    MainClass.handleException(null, ex);
                } finally {
                    callback.update();
                }
            }
        }.execute();
    }

    void editComments(final Blog blog, final ArrayList<WordPressComment> comments) {
        new SwingWorker<Object,Object>() {
            @Override
            protected Object doInBackground() throws Exception {
                for (WordPressComment c : comments) {
                    account.editComment(blog, c);
                }
                return null;
            }

            @Override
            protected void done() {
                try {
                    get();
                } catch (InterruptedException | ExecutionException ex) {
                    MainClass.handleException(null, ex);
                } finally {
                    callback.update();
                }
            }
        }.execute();
    }

    void editComment(final Blog blog, final WordPressComment comment) {
        new SwingWorker<Object,Object>() {
            @Override
            protected Object doInBackground() throws Exception {
                account.editComment(blog, comment);
                return null;
            }

            @Override
            protected void done() {
                try {
                    get();
                } catch (InterruptedException | ExecutionException ex) {
                    MainClass.handleException(null, ex);
                } finally {
                    callback.update();
                }
            }
        }.execute();
    }

    void newComment(final Blog blog, final String entryId, final WordPressComment comment, final int parentId) {
        new SwingWorker<Object,Object>() {
            @Override
            protected Object doInBackground() throws Exception {
                account.newComment(blog, entryId, comment, parentId);
                return null;
            }

            @Override
            protected void done() {
                try {
                    get();
                } catch (InterruptedException | ExecutionException ex) {
                    MainClass.handleException(null, ex);
                } finally {
                    callback.update();
                }
            }
        }.execute();
    }
}
